$(function(){
	var obj = {
		newMan:$('.advment'),				//新人专享
		oldPrice:$('.oldPrice'),			//原价
		shopCar:$('.shopingCar_red')		//购物车右上角
	}
	var flag = true;
    function Flag(a){
        return flag = a;
    }

	// 进入首页判断购物车是否有商品
	jduge();
	function jduge(){
		$.ajax({
			type:"post",
			url:"http://a.91jfk.com/Product/Shopindex/ShowCartNum",
			async:true,
			data:{
				classify:1
			},
			dataType:'json',
			success:function(res){
				console.log(res);
				if (res.num == 0) {
					//obj.shopCar.hide();
				} else {
					//obj.shopCar.children('span').text(res.num);
					var foot_Con = $('.foot_Con');
					var span = $('<span class="shopingCar_red"><span>'+res.num+'</span>+</span>');
					foot_Con.append(span);
				}
			},
			error:function(err){
				console.log(err);
			}
		});
	}
	// 进入页面首先加载轮播图
	loadSwiper();
	function loadSwiper(){
		$.ajax({
			type:"post",
			url:"http://a.91jfk.com/Product/Pic/index",
			async:true,
			data:{
				classify:1
			},
			dataType:'json',
			success:function(res){
				if(res.code == '1'){
					var msg = res.msg;
					var imgArr = [];
					for(var i = 0;i < msg.length;i++){
						imgArr.push(msg[i].pic);
					}
					// 轮播图
					var Swiper = new SwiperLunBo();
					Swiper.carousel(imgArr).swiper().changeLunBo(imgArr);		//changeLunBo方法只能在swiperLunBo方法之后调用
				}else{
					console.log(123);
				}
			},
			error:function(err){
				console.log(err);
			}
		});
	}
	
	//进入页面判断用户是不是新用户，新用户显示新人专题，老用户不显示
	judge();
	function judge(){
		$.ajax({
			type:"get",
			url:"http://a.91jfk.com/Product/Shopindex/control_new_exclusive",
			dataType:'json',
			success:function(res){
				console.log(res);
				if (res.code == '1') {
					obj.newMan.show();
				} else{
					obj.newMan.hide();
				}
			},
			error:function(err){
				console.log(err)
			}
		});
	}
	
	//进入页面加载精选专题的数据
	Selected();
	function Selected(){
		$.ajax({
			url:"http://a.91jfk.com/Product/Shopindex/select_topics_index",
			type:'get',
			dataType:'json',
			success:function(res){
				console.log(res);
				if (res.code == '1') {
					var msg = res.message;
					for (var i = 0;i < msg.length;i++) {
						selectedShow(msg[i].id,msg[i].goods_type,msg[i].show_pic,msg[i].introduce,msg[i].pname);
						$("img.IMG.lazy").lazyload({
							placeholder:"/Public/Product/img/loading2.gif",
							effect: "fadeIn",
							container: $(".slide_x")
						});
					}
				}
			},
			error:function(err){
				console.log(err);
			}
		});
	}
	
	// 进入页面加载精选专题的数据 
	loadYouLike();
    function loadYouLike() {
        var page = 1; 		// 页码数
    	$.ajax({			// 进入页默认加载第一页，上拉的时候在加载第二页
            type: 'GET',
            url: 'http://a.91jfk.com/Product/Shopindex/guess_you_like?p=' + page,
            dataType: 'json',
            success: function(res) {
            	console.log(res);
            	if (res.code == '1') {
                    var data = res.message;
                    for(var i = 0;i < data.length;i++){ 
                        var msg = res.message;
						for (var i = 0;i < msg.length;i++) {
							msg[i].is_selling == '1' ? hot= 'HOT' : hot = '';
							msg[i].is_jingxuna == '1' ? jing = '精选' : jing = '';
							youLikeShow(msg[i].id,msg[i].goods_type,res.nowpage,msg[i].show_pic,hot,jing,msg[i].pname,msg[i].low_price,msg[i].original_price);
							msg[i].original_price == '0.00' ? $('.oldPrice').eq(i).hide() : $('.oldPrice').eq(i).show();
							$('img.lazy'+res.nowpage).lazyload({
								placeholder:"/Public/Product/img/loading2.gif",
								effect: "fadeIn",
								container: $(".list_row")
							});
						}
                    }
                }
            	if (res.nowpage < res.total) {	// 当默认加载第一页获取当前页和总页数，判断当前页小于总页数再去加载，否则直接显示没有更多数据
			        // dropload函数接口设置
			        $('.jfGoodsList_box').dropload({
			            scrollArea: window,
			            // 上拉加载更多
			            domDown: {
			                domClass: 'dropload-down',
			                // 滑动到底部显示内容
			                domRefresh: '<div class="dropload-refresh">上拉加载更多</div>',
			                // 内容加载过程中显示内容
			                domLoad: '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
			                // 没有更多内容显示提示
			                domNoData: '<div class="dropload-noData">我也是有深度的...</div>'
			            },
			            // 上拉加载更多 回调函数
			            loadDownFn: function(me) {
			                page++; // 每次请求，页码加1 
			                if(flag){
			                    var load1 = $.ajax({
			                        type: 'GET',
			                        url: 'http://a.91jfk.com/Product/Shopindex/guess_you_like?p=' + page,
			                        dataType: 'json',
			                        success: function(res) {
			                            console.log(res);
			                            if (res.nowpage < res.total) {
			                                Flag(true);
			                                if (res.code == '1') {
			                                    var data = res.message;
			                                    for(var i = 0;i < data.length;i++){ 
			                                        var msg = res.message;
													for (var i = 0;i < msg.length;i++) {
														msg[i].is_selling == '1' ? hot= 'HOT' : hot = '';
														msg[i].is_jingxuna == '1' ? jing = '精选' : jing = '';
														youLikeShow(msg[i].id,msg[i].goods_type,res.nowpage,msg[i].show_pic,hot,jing,msg[i].pname,msg[i].low_price,msg[i].original_price);
														msg[i].original_price == '0.00' ? $('.oldPrice').eq(i).hide() : $('.oldPrice').eq(i).show();
														$('img.lazy'+res.nowpage).lazyload({
															placeholder:"/Public/Product/img/loading2.gif",
															effect: "fadeIn",
															container: $(".list_row")
														});
													}
			                                    }
			                                    // 重置
			                                    setTimeout(function() {
			                                        me.resetload();
			                                    }, 1500);
			                                } else {
			                                    me.noData();
			                                    me.lock();
			                                    // 重置
			                                    setTimeout(function() {
			                                        me.resetload();
			                                    }, 1500);
			                                }
			                            }else{
			                                if (res.code == '1') {
			                                    var data = res.message;
			                                    for(var i = 0;i < data.length;i++){
			                                        var msg = res.message;
													for (var i = 0;i < msg.length;i++) {
														msg[i].is_selling == '1' ? hot= 'HOT' : hot = '';
														msg[i].is_jingxuna == '1' ? jing = '精选' : jing = '';
														youLikeShow(msg[i].id,msg[i].goods_type,res.nowpage,msg[i].show_pic,hot,jing,msg[i].pname,msg[i].low_price,msg[i].original_price);
														msg[i].original_price == '0.00' ? $('.oldPrice').eq(i).hide() : $('.oldPrice').eq(i).show();
														$('img.lazy'+res.nowpage).lazyload({
															placeholder:"/Public/Product/img/loading2.gif",
															effect: "fadeIn",
															container: $(".list_row")
														});
													}
			                                    }
			                                    // 重置
			                                    setTimeout(function() {
			                                        me.resetload();
			                                    }, 1500);
			                                } else {
			                                    me.noData();
			                                    me.lock();
			                                    // 重置
			                                    setTimeout(function() {
			                                        me.resetload();
			                                    }, 1500);
			                                }
			                                load1.abort();
			                                Flag(false);
			                                me.noData();
			                                me.lock();
			                                // 重置
			                                setTimeout(function() {
			                                    me.resetload();
			                                }, 1500);
			                            }
			                        },
			                        // 加载出错
			                        error: function(err) {
			                            console.log('Ajax error!');
			                            // 即使加载出错，也得重置
			                            setTimeout(function() {
			                                me.resetload();
			                            }, 1500);
			                        }
			                    });
			                }else{
			                    // 再往下已经没有数据，锁定
			                    // 显示无数据
			                    me.noData();
			                    me.lock();
			                    // 重置
			                    setTimeout(function() {
			                        me.resetload();
			                    }, 1500);
			                }
			            }
			        });
            	} else{
            		$('#noData').show();
            	}
            },
            error:function(err){
            	console.log(err);
            }
        });
    }
	
    //点击消息按钮跳转到消息界面查看消息
    $('.news').bind('click',function(){
    	console.log('查看消息...');
    	//window.location.href = '';
    });
    
    //点击集分理财跳转集分理财页面
    $('.manage').bind('click',function(){
    	console.log('跳转集分理财...');
    	//window.location.href = '';
    });
    //点击集分游戏跳转集分游戏页面
    $('.game').bind('click',function(){
    	console.log('跳转集分游戏...');
    	window.location.href = 'GameIndex.html';
    });
    
    //点击查看更多的事件
    $('.lookMore').bind('click',function(){
    	console.log('查看更多...');
    	window.location.href = 'jingxuan.html';
    });
    
    //点击精选专题商品，跳转页面
    $('.roll_x').on('click','.sld',function(){
    	var goodId = $(this).find('input[name=goodId]').val();
		var goodType = $(this).find('input[name=goodType]').val();
    	if (goodType == '1') {
			window.location.href = "/Product/Pointshop/point_shop_goodDetail.html?id=" + goodId;
		} else if(goodType == '2'){
			window.location.href = "/Product/Pointexchange/jfdhDetail.html?id=" + goodId;
		}
    });
    
    //点击猜你喜欢商品，跳转页面
    $('.list_row').on('click','.list_col',function(){
		var goodId = $(this).find('input[name=youLikeId]').val();
		var goodType = $(this).find('input[name=youLikeType]').val();
    	if (goodType == '1') {
			window.location.href = "/Product/Pointshop/point_shop_goodDetail.html?id=" + goodId;
		} else if(goodType == '2'){
			window.location.href = "/Product/Pointexchange/jfdhDetail.html?id=" + goodId;
		}
    });
    
    //点击底部的table-bar跳转页面，更改图标
    $('.home_foot').delegate('.foot_Con','click',function(){
    	window.location.href = '/Product/Zero/gouwuche.html'
    });
    
    //回到顶部
    $(window).scroll(function(){
	    var sc = $(window).scrollTop();
	    if(sc > 200){
	    	$(".goTop").css("display","block");
	    }else{
	   		$(".goTop").css("display","none");
	    }
	 });
	 $(".goTop").click(function(){
	    var sc = $(window).scrollTop();
	    $('body,html').animate({
	    	scrollTop:0
	    },500);
	 });
	
	//精选专题的数据
	function selectedShow(id,type,src,attr,title){
		var roll_x = $('.roll_x');
		var show = $('<div class="sld"><input type="hidden" name="goodId" value="'+id+'"/><input type="hidden" name="goodType" value="'+type+'"/><div class="sld_img_box"><img class="IMG lazy" src="/Public/Product/img/loading2.gif" data-original="'+src+'"/></div><div class="arrt">'+attr+'</div><p>'+title+'</p></div>');
		roll_x.append(show);
	}
	
	//猜你喜欢商品数据
	function youLikeShow(id,type,j,src,hot,jing,title,newP,oldP){
		var list_row = $('.list_row');
		var list_td = $('<div class="list_col"><input type="hidden" name="youLikeId" value="'+id+'"/><input type="hidden" name="youLikeType" value="'+type+'"/><div class="img_box"><img class="IMG lazy'+ j +'" src="/Public/Product/img/loading2.gif" data-original="'+src+'"/></div><p class="good_tit"><span class="redBg">'+hot+'</span><span class="redBg">'+jing+'</span>'+title+'</p><div class="priceORjf"><p><span class="newPrice">&yen;'+newP+'</span>&nbsp;<span class="oldPrice">&yen;'+oldP+'</span></p></div></div>');
		list_row.append(list_td);
	}
});